package com.vue.vuedemo.Controller;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.vue.vuedemo.pojo.Comment;
import com.vue.vuedemo.pojo.Sportscircle;
import com.vue.vuedemo.service.CommentService;
import com.vue.vuedemo.service.SportsCircleService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@RestController
@JsonIgnoreProperties({"hibernateLazyInitializer","handler"})
@CrossOrigin(origins = {"http://localhost:8080"}) //当前网站向另一个网站请求数据就是跨域
@Controller
public class CommentController {


    @Resource
    CommentService commentService;

    @Resource
    SportsCircleService sportsCircleService;

    //根据评论查询帖子
    @GetMapping(value = "CommentFindAll")
    public List<Comment> findAll(Integer sId){
        List<Comment> list =  commentService.findAll(sId);
        return list;
    }

    public Integer num;

    //修改帖子评论喜欢 +1
    @GetMapping(value = "CommentUpdateZan")
    public Integer updateZan(Integer cId){
        try {
            commentService.updateZan(cId);
            num = 1; //成功
        }catch (Exception ex){
            num = 2; //失败
        }
        return num;
    }

    //添加帖子评论
    @GetMapping(value = "CommentAdd")
    public Integer commentAdd(Integer cSid,Integer sPid ,String cContext,Comment comment, HttpSession session){
//            Integer pid =(Integer) session.getAttribute("userId");
//            comment.setCPid(pid);
            comment.setCSid(cSid);
            comment.setCContext(cContext);
            comment.setCPid(sPid);
        try {
            commentService.addComment(comment);
            num = 1; //成功
        }catch (Exception ex){
            num = 2; //失败
            System.out.print(ex);
        }
        return num;
    }


    //根据用户编号查询评论
    @GetMapping(value = "CommentPidFindAll")
    public List<Comment> pIdFindAll(Integer pId,HttpSession session){
        List<Comment> list =  commentService.pIdFindAll(pId);
        return list;
    }

    //删除评论
    @GetMapping(value = "CommentDel")
    public Integer commentDel(Integer cId, HttpSession session){
        try {
            commentService.delComment(cId);
            num = 1; //成功
        }catch (Exception ex){
            num = 2; //失败
            System.out.print(ex);
        }
        return num;
    }


}
